// APP
if (window.plus) {
    scanHistoryPageReady();
} else {
    document.addEventListener('plusready', scanHistoryPageReady, false);
}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        scanHistoryPageReady();
    }
});

function scanHistoryPageReady() {
    var scanHistoryPage = function () {
        this.init();
    };

    scanHistoryPage.prototype = {
        // 初始化
        init: function () {
            this.paintpage();
            this.bindEvent();
        },

        // 画页面
        paintpage: function () {
            var model = this;
            $("#historycontainer").html("");
            var list = getLocalStorage(STORAGE_MANAGER.barcodehistory);
            if (!list || list.length == 0) {
                $("#nodatadiv").show();
                return;
            }

            // 倒序
            list = list.reverse();
            if (list.length > 10) {
                list.splice(10);
            }
            setLocalStorage(STORAGE_MANAGER.barcodehistory, list);
            var html = [];
            console.log(JSON.stringify(list));
            $.each(list, function (i, item) {
                var typeStr = model.getTypeStr(item.type);
                var img = (item.type == 1 || item.type == 2) ? getImageUrl(item.logo, 350, 350) : "../../images/place/350x350.png";
                html.push("<div class='overflow mbbottom' name='historyItem' tip='" + item.type + "|" + item.result + "'>");
                html.push("<div class='images-box'><img src='" + img + "' /></div>");
                html.push("<div class='images-con'>");
                html.push("<p class='images-title'>" + typeStr + item.result + "</p>");
                html.push("<p class='images-writing'>" + item.desc + "</p>");
                html.push("<p class='images-time'>" + dateDiff(item.create_time) + "</p>");
                html.push("</div></div>");
            });
            $("#historycontainer").html(html.join(""));
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 返回
            $("#btnback").on("click", function () {
            		plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(0)");
//              back();
//              if (os.ios) {
//                  plus.navigator.setStatusBarBackground("rgba(0,0,0,0.97)");
//                  plus.navigator.setStatusBarStyle('UIStatusBarStyleBlackOpaque');
//              }
            });

            // 清空
            $("#btnclean").on("click", function () {
                setLocalStorage(STORAGE_MANAGER.barcodehistory, []);
                model.paintpage();
            });

            $("#historycontainer").on("click", "div[name=historyItem]", function () {
                var tips = $(this).attr("tip");
                var type = tips.split('|')[0];
                var result = tips.split('|')[1];
                if (type == 1 || type == 2) {
                    // 商品条形码
                    redirect("../goods/goodsdetail.html?id=" + result + "&type=" + type);
                } else if (type == 3) {
                    // 商品条形码
                    redirect("../order/detail.html?id=" + result + "&type=" + type);
                }
            });
        },

        // 条码类型描述
        getTypeStr: function (type) {
            var typeStr = "";
            switch (type) {
                // 商品条码
                case  1:
                    typeStr = "商品条码";
                    break;
                // 商品二维码
                case  2:
                    typeStr = "商品二维码";
                    break;
                // 订单条码
                case  3:
                    typeStr = "订单条码";
                    break;
                // 活动二维码
                case  4:
                    typeStr = "活动二维码";
                    break;
                default:
                    break;
            }

            return typeStr;
        }
    };

    var _scanHistoryPage = new scanHistoryPage();
}